package com.example.gaojt.androiddemo.jsoupStudy;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by gaojt on 6/20/17.
 */

public class JsoupStudyActivity extends Activity {
    private static final String TAG = "JsoupStudyActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String aa = extractTuDouVideo("http://video.tudou.com/v/XMjgwOTczNjMwMA==.html?spm=a2h28.8313475.c1.dimg1&t=1&recoid=160998804177031394&itemid=5456513056647734696&seccateId=10016");
                    Log.d(TAG, "run: aa = " + aa);
                } catch (Exception e) {
                    Log.w(TAG, "run: ", e);

                }
            }
        }).start();
    }

    public static String extractTuDouVideo(String url) throws Exception {

        Document doc = Jsoup.connect(url).timeout(6000).get();
        String title = doc.select("#vcate_title").text();
        /**
         * 获取视频地址
         */
        Pattern pattern = Pattern.compile("iid\\\\:.+?\n");
        String content = doc.html();
        String id = "";
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            id = matcher.group(0).replace("iid:", "");
            break;
        }

        String videoAddr = "http://www.tudou.com/v/" + id + "/v.swf";
        return videoAddr;
    }
}
